package com.kgc.controller;

import com.alibaba.fastjson.JSONObject;
import com.kgc.entity.Classes;
import com.kgc.entity.Faqs;
import com.kgc.service.FaqsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FaqsController {
    @Autowired
    private FaqsService service;
    @RequestMapping("/list")
    @ResponseBody
    public String list(String cname){
        List<Faqs> list = service.listFaqs(cname);
        return JSONObject.toJSONString(list);
    }
    @RequestMapping("/add")
    public String add(){
        return "add.jsp";
    }
    @RequestMapping("/listClasses")
    @ResponseBody
    public String listl(){
        List<Classes> list = service.listClasses();
        return JSONObject.toJSONString(list);
    }
    @RequestMapping("/insert")
    public String insert(Model model,Faqs faqs){
        int count = service.insert(faqs);
        model.addAttribute("count",count);
        return "index.jsp";
    }
    @RequestMapping("/getselect")
     public String select(int id, Model model){
        Faqs faqs= service.select(id);
        model.addAttribute("faqs",faqs);
        return "show.jsp";
    }
    @RequestMapping("/count")
    @ResponseBody
    public String count(String title){
        int count = service.count(title);
        Map<String,Object> map=new HashMap<>();
        map.put("count",count);
        return JSONObject.toJSONString(map);
    }
    @RequestMapping("/countByname")
    @ResponseBody
    public String countByName(String cname){
        int count = service.countByName(cname);
        Map<String,Object> map=new HashMap<>();
        map.put("count",count);
        return JSONObject.toJSONString(map);
    }
}
